/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.gapvis.koichi;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import at.tugraz.kmi.css.analyzer.UserCompetence;
import at.tugraz.kmi.css.analyzer.gapvis.GraphEdge;
import at.tugraz.kmi.css.analyzer.gapvis.koichi.events.MenuPointListener;
import at.tugraz.kmi.css.analyzer.gapvis.koichi.events.VertexMenuListener;
import edu.uci.ics.jung.visualization.VisualizationViewer;

class CompetenceVertexPopup extends JPopupMenu {
	private static final long						serialVersionUID	= 1L;
	private final KiCurrentCompetenceProfilePane	owner;
	private final CommentMenuItem					commentMenuItem;

	public CompetenceVertexPopup(JFrame frame,
			KiCurrentCompetenceProfilePane owner) {
		super("Competence Vertex Menu");
		this.owner = owner;
		commentMenuItem = new CommentMenuItem();

		this.add(new EditCompetenceMenuItem(frame, owner));
		this.add(new JSeparator());
		this.add(commentMenuItem);
		this.add(new JSeparator());
		this.add(new DeleteCompetenceMenuItem());
	}

	class DeleteCompetenceMenuItem extends JMenuItem implements
			VertexMenuListener<UserCompetence> {
		private static final long								serialVersionUID	= 1L;
		private UserCompetence									competence;
		private VisualizationViewer<UserCompetence, GraphEdge>	viewer;

		public DeleteCompetenceMenuItem() {
			super("Delete");
			addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					owner.removeCompetence(competence);
				}
			});
		}

		@Override
		public void setVertexAndView(UserCompetence c,
				VisualizationViewer viewer) {
			competence = c;
			this.viewer = viewer;
		}
	}

	class CommentMenuItem extends JMenuItem implements
			VertexMenuListener<UserCompetence> {
		private static final long							serialVersionUID	= 1L;
		private UserCompetence								competence			= null;
		private VisualizationViewer<UserCompetence, String>	viewer				= null;

		public CommentMenuItem() {
			super();

			addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (competence != null) {
						owner.switchVisibleComment(competence);
					}
				}
			});
		}

		@Override
		public void setVertexAndView(UserCompetence c,
				VisualizationViewer viewer) {
			competence = c;
			this.viewer = viewer;
			if (c != null) {
				String comment = c.getComment();
				setEnabled(comment != null && (comment.length() > 0));
				setText(c.isCommentVisible() ? "Hide comment" : "Show comment");
			}
		}
	}

	static class EditCompetenceMenuItem extends JMenuItem implements
			VertexMenuListener<UserCompetence>, MenuPointListener {
		private static final long							serialVersionUID	= 1L;
		private UserCompetence								competence;
		private VisualizationViewer<UserCompetence, String>	viewer;
		private Point2D										point;
		private final JFrame								frame;
		private final KiCurrentCompetenceProfilePane		owner;

		public EditCompetenceMenuItem(JFrame f,
				KiCurrentCompetenceProfilePane cp) {
			super("Edit properties");
			frame = f;
			owner = cp;
			addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					CompetenceVertexDialog dialog = new CompetenceVertexDialog(
							frame, competence, owner);
					dialog.setLocation((int) point.getX(), (int) point.getY());
					dialog.setVisible(true);
				}
			});
		}

		@Override
		public void setVertexAndView(UserCompetence c,
				VisualizationViewer viewer) {
			competence = c;
			this.viewer = viewer;
		}

		@Override
		public void setPoint(Point2D point) {
			this.point = point;
		}
	}
}